package com.replication.jdbc;

import java.sql.Connection;
import java.sql.SQLException;

public abstract class AbstructConnectionSelector implements ConnectionSelector {

	// private static final String[] MASTER_PERFIX =
	// {"CREATE ","UPDATE ","DELETE"};

	public Connection select(ReplicationConnectionProxy replicationConn, String sql) throws SQLException {
		if (isMasterOnly(sql)) {
			return replicationConn.getMasterConnection();
		} else {
			return replicationConn.nextSlaverConnection();
		}

	}

	protected abstract boolean isMasterOnly(String sql);

	protected static boolean existWords(String searchIn, String searchFor) {
		return searchIn.indexOf(searchFor) >= 0;
	}

	protected static boolean isSelectFrom(String sql) {
		String upperSql = sql.trim().toUpperCase();

		return (upperSql.startsWith("SELECT")) && (existWords(upperSql, " FROM "));
	}
}
